package com.open.validator.impl;

import org.hibernate.validator.internal.constraintvalidators.bv.number.InfinityNumberComparatorHelper;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.OptionalInt;

/**
 * @author: system
 * @Date: 2024/4/19 15:10
 * @Version: V1.0
 * @description: desc:
 */

final class NumberComparatorHelper {

    private NumberComparatorHelper() {
    }

    public static int compare(BigDecimal number, long value) {
        return number.compareTo(BigDecimal.valueOf(value));
    }

    public static int compare(BigInteger number, long value) {
        return number.compareTo(BigInteger.valueOf(value));
    }

    public static int compare(Long number, long value) {
        return number.compareTo(value);
    }

    public static int compare(Number number, long value) {
        return Long.compare(number.longValue(), value);
    }

    public static int compare(Double number, long value, OptionalInt treatNanAs) {
        OptionalInt infinity = InfinityNumberComparatorHelper.infinityCheck(number, treatNanAs);
        if (infinity.isPresent()) {
            return infinity.getAsInt();
        }
        return Long.compare(number.longValue(), value);
    }

    public static int compare(Float number, long value, OptionalInt treatNanAs) {
        OptionalInt infinity = InfinityNumberComparatorHelper.infinityCheck(number, treatNanAs);
        if (infinity.isPresent()) {
            return infinity.getAsInt();
        }
        return Long.compare(number.longValue(), value);
    }


    public static void main(String[] args) {
        int compare = NumberComparatorHelper.compare(123.5f, 123,
                InfinityNumberComparatorHelper.GREATER_THAN);
        System.out.println(compare);

    }
}
